const api = require('../../../api/mop_api');
import { toast, to, err } from '../../../utils/wxapi';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    size: 20,
    pageTotal: 0,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.boaed();
  },
  async boaed() {
    try {
      const { page, size, list } = this.data;
      const res = await api.board_list({ page, size });
      const { respCode, source, pageTotal, msg } = res;
      if (respCode === 200) {
        const newlist = JSON.parse(source);
        if (list.length === 0) {
          this.setData({ list: newlist, pageTotal });
        } else {
          this.setData({ list: list.concat(newlist), pageTotal });
        }
      } else {
        toast(msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  todel(e) {
    const { id } = e.currentTarget.dataset;
    to('/dj/pages/delboard/delboard?id=' + id);
  },
  add() {
    to('/dj/pages/addboard/addboard');
  },
  onHide() {
    this.setData({
      list: [],
      page: 1,
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { list, pageTotal, page } = this.data;
    if (list.length < pageTotal) {
      this.setData({ page: page + 1 });
      this.boaed();
    } else {
      toast('已加载全部数据');
    }
  },
});
